function request(method, url, param, ContentType, showLoading) {
	//返回一个Promise对象
	return new Promise(function(resolve, reject) {
		if (showLoading === true || showLoading === undefined) {
			uni.showLoading({
				title: '加载中',
				mask: true
			});
		}
		uni.request({
			url: url,
			method: method,
			data: param,
			header: {
				'Content-Type': ProcessingRequestHeader(method, ContentType),
				'Accept': 'application/json',
				"satoken": uni.getStorageSync('satoken')
			},
			success: function(res) {
				if (res.statusCode === 404) {
					uni.hideLoading()
					uni.stopPullDownRefresh();
					uni.showToast({
						title: '错误状态码:404',
						icon: 'none',
						duration: 2000
					});
					return
					// 请求状态码500
				} else if (res.statusCode === 502) {
					uni.hideLoading()
					uni.stopPullDownRefresh();
					uni.showToast({
						title: '服务器错误:502',
						icon: 'none',
						duration: 2000
					});
					return
					// 请求状态码500
				} else if (res.statusCode === 500) {
					uni.hideLoading()
					uni.stopPullDownRefresh();
					uni.showToast({
						title: '服务器错误:500',
						icon: 'none',
						duration: 2000
					});
					return
				} else if (res.data.code === 500) {
					uni.hideLoading()
					uni.stopPullDownRefresh();
					uni.showToast({
						title: res.data.msg,
						icon: 'none',
						duration: 5000
					});
					// 登录失效跳转登录页面
					if (res.data.msg === '登录状态错误，请重新登录') {
						setTimeout(function() {
							uni.reLaunch({
								url: "/pages/login/index"
							})
						}, 1000)
					}
				} else if (res.data.code === 200) {
					uni.hideLoading()
					uni.stopPullDownRefresh();
					resolve(res.data.data);
				} else {
					uni.hideLoading()
					uni.stopPullDownRefresh();
					uni.showToast({
						title: res.data.msg,
						icon: 'none',
						duration: 3000
					});
					reject(res.data.msg)
				}
			},
			fail: function(res) {
				console.log(res, url)
				uni.hideLoading()
				uni.showToast({
					title: '服务连接异常',
					icon: 'none',
					duration: 2000
				});
			}
		});
	});
}

// #ifdef H5
var url = ""
// #endif
// #ifdef MP-WEIXIN
var url = "https://api.aizyiot.cn:6060"
// #endif

// get请求
function get(path, param = {}, ContentType, showLoading) {
	return request("GET", url + path, param, ContentType, showLoading);
}

// post请求
function post(path, param = {}, ContentType, showLoading) {
	return request("POST", url + path, param, ContentType, showLoading);
}

// del请求
function del(path, param = {}, ContentType, showLoading) {
	return request("DELETE", url + path, param, ContentType, showLoading);
}

// PUT请求
function put(path, param = {}, ContentType, showLoading) {
	return request("PUT", url + path, param, ContentType, showLoading);
}

// 图片上传
function uploadFile(path, param) {
	//返回一个Promise对象
	return new Promise(function(resolve, reject) {
		uni.showLoading({
			title: '加载中',
			mask: true
		});
		console.log(123123, param)
		uni.uploadFile({
			url: url + path,
			filePath: param.file.path,
			name: 'file',
			formData: param,
			header: {
				"satoken": uni.getStorageSync('satoken')
			},
			success: (res) => {
				let data = JSON.parse(res.data)
				if (data.code === 200) {
					uni.hideLoading()
					resolve(data.data);
				} else {
					uni.showToast({
						title: data.msg,
						icon: 'none',
						duration: 2000
					});
				}
			},
			fail(err) {
				console.log(err)
			},
			error(error) {
				uni.showToast({
					title: '上传失败',
					icon: 'none',
					duration: 2000
				});
			}
		});
	});
}

// 文件下载
function downloadFile(path, param) {
	uni.request({
		url: url + path, // 替换为您的目标 URL
		method: 'POST', // 指定请求方法为 POST
		data: param,
		success: function(res) {
			console.log(res)
			if (res.statusCode === 200) {
				// 请求成功，将文件保存到本地
				uni.saveFile({
					tempFilePath: res.data,
					success: function(saveRes) {
						console.log(saveRes)
						// 保存文件成功后的回调
						// var savedFilePath = saveRes.savedFilePath;

						// // 打开保存的文件
						// uni.openDocument({
						// 	filePath: savedFilePath,
						// 	success: function(openRes) {
						// 		console.log('文件打开成功');
						// 	},
						// 	fail: function(error) {
						// 		console.log('文件打开失败', error);
						// 	}
						// });
					},
					fail: function(error) {
						console.log('文件保存失败', error);
					}
				});
			}
		}
	});

}



// 处理请求头
function ProcessingRequestHeader(method, ContentType) {
	if (method === 'GET' || method === 'DELETE') {
		return 'application/json; charset=utf-8'
	}

	if (ContentType) {
		return ContentType
	} else {
		return "application/x-www-form-urlencoded"
	}
}



module.exports = {
	get: get,
	post: post,
	del: del,
	put: put,
	uploadFile: uploadFile,
	downloadFile: downloadFile,
}